<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2015 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Newskey.php 88712 2018-09-04 09:27:53 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_newskey
 * +----------------------------------------------------------------------
 */

namespace application\admin\controller;

use ticky\request;
use ticky\response;

class newskey extends base {

    //首页
    public function index() {
        $search = $this->search_frm();
        $ret = db('newskey')->where($search['sql'])->order('id desc')->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->assign('search', $search['arr']);
        $this->display('newskey/index');
    }

    // 批量删除
    public function batchremove() {
        $optype = request::post('optype', '');
        if ($optype == "del") {
            $ids = request::post('ids', []);
            db('newskey')->delete('id in(' . implode(",", $ids) . ')');
            showmsg('删除成功', '/admin/newskey');
        } else {
            if (is_array($_GET['name'])) {
                foreach ($_GET['name'] as $id => $val) {
                    $data = array();
                    $data['name'] = $_GET['name'][$id];
                    $data['url'] = $_GET['url'][$id];
                    db('newskey')->where(['id' => $id])->update($data);
                }
            }
            if (is_array($_GET['newname'])) {
                foreach ($_GET['newname'] as $key => $value) {
                    if ($value) {
                        $data = array();
                        $data['name'] = $value;
                        $data['url'] = $_GET['newurl'][$key];
                        db('newskey')->where(['id' => $id])->insert($data);
                    }
                }
            }
            showmsg('关联链接更新成功', '/admin/newskey');
        }
    }

    //查询条件
    private function search_frm() {
        $search = request::get('search', []);
        $where = '1=1 ';

        if ($keyid = trim($search['id'])) {
            $where .= "and id = '{$keyid}' ";
        }

        if ($keyname = trim($search['name'])) {
            $where .= "and name = '{$keyname}' ";
        }

        if ($keyurl = trim($search['url'])) {
            $where .= "and url = '{$keyurl}' ";
        }

        return [
            'arr' => $search,
            'sql' => $where
        ];
    }

}
